package com.xhwl.logistics.service.cent;

import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.sdata.Project;
import com.xhwl.common.pojo.cent.sdata.Region;
import com.xhwl.common.query.cent.sdata.ProjectQuery;
import com.xhwl.logistics.bo.cent.ProjectBO;
import com.xhwl.logistics.component.VehicleHomeAddressProperty;
import com.xhwl.logistics.converter.mapstruct.CentProjectConverter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

//@Service
public class QueryProjectService {

    @Autowired
    private VehicleHomeAddressProperty vehicleHomeAddressProperty;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private CentProjectConverter centProjectConverter;

    public String queryProjectLocalPlateName(Integer projectId) {

        ProjectQuery projectQuery = new ProjectQuery();
        projectQuery.setIds(Collections.singletonList(projectId));

        Project project = projectService.getOne(projectQuery, null);
        if (null==project) {
            throw new BusinessException("根据项目Id找不到项目");
        }
        Integer provinceId = project.getProvinceId();
        Integer cityId = project.getCityId();
        if (null==provinceId || null==cityId) {
            throw new BusinessException("根据项目的省级id或者市级id存在空值，项目地址信息不完整，无法判断归属地");
        }

        List<Region> regionList = regionService.findByIds(Arrays.asList(provinceId, cityId));
        StringBuilder homeAddress = new StringBuilder();
        if (regionList.size()==2) {
            regionList.forEach(region -> {
                Integer level = region.getLevel();
                if (level.equals(1)) {
                    String name = region.getName();
                    homeAddress.append(name);
                }
            });
            regionList.forEach(region -> {
                Integer level = region.getLevel();
                if (level.equals(2)) {
                    String name = region.getName();
                    homeAddress.append(name);
                }
            });
        } else {
            throw new BusinessException("根据项省级id和市级id查询地区信息出错");
        }

        return vehicleHomeAddressProperty.getLocalPlateName(homeAddress.toString());
    }

    public List<ProjectBO> queryProjects(List<Integer> projectIds) {
        ProjectQuery projectQuery = new ProjectQuery();
        projectQuery.setIds(projectIds);

        List<Project> projectList = projectService.list(projectQuery, null);

        return projectList.stream().map(project -> centProjectConverter.project2ProjectBO(project)).collect(Collectors.toList());
    }

    public ProjectBO queryProject(Integer projectId) {
        ProjectQuery projectQuery = new ProjectQuery();
        projectQuery.setIds(Collections.singletonList(projectId));

        return centProjectConverter.project2ProjectBO(projectService.getOne(projectQuery, null));
    }

}
